/**
 * 
 * @authors cherish yii2 (cherish@cherish.pw)
 * @date    2021-07-28 12:48:28
 * @version v2.0
 * @description the core js of todolist project
 * 
 * ━━━━━━神兽出没━━━━━━
 * 　　   ┏┓　 ┏┓
 * 　┏━━━━┛┻━━━┛┻━━━┓
 * 　┃              ┃
 * 　┃       ━　    ┃
 * 　┃　  ┳┛ 　┗┳   ┃
 * 　┃              ┃
 * 　┃       ┻　    ┃
 * 　┃              ┃
 * 　┗━━━┓      ┏━━━┛ Code is far away from bugs with the animal protecting.
 *       ┃      ┃     神兽保佑,代码无bug。
 *       ┃      ┃
 *       ┃      ┗━━━┓
 *       ┃      　　┣┓
 *       ┃      　　┏┛
 *       ┗━┓┓┏━━┳┓┏━┛
 *     　  ┃┫┫　┃┫┫
 *     　  ┗┻┛　┗┻┛
 *
 * ━━━━━━感觉萌萌哒━━━━━━
 */

// 请根据考试说明文档中列出的需求进行作答
// 预祝各位顺利通过本次考试！
// ...

const users = JSON.parse(window.localStorage.getItem('users')) || []
const form = document.querySelector('form')
const addToDo = document.querySelector('#title')
const ol = document.querySelector('ol')
const span = document.querySelector('#todocount')
const ul = document.querySelector('ul')
const olli = document.querySelector('ol input')
const doingBox = document.querySelector('.demo-box')

const one = document.querySelector('.one')
const two = document.querySelector('.two')
const three = document.querySelector('.three')

/**
 * 添加代办事项
 */
add()
function add(){
  let str = ''
  users.forEach(function (item) {
    str += `
      <li>
        <input type="checkbox" />
        <p onclick="edit(1)">${item.res}</p >
        <a class="remove1" href=" "></ a>
      </li>
    `
  })
  ol.innerHTML += str
  window.localStorage.setItem('users', JSON.stringify(users))
}
form.addEventListener('submit', function (e) {
  e = e || window.event
  try { e.preventDefault() } catch (err) { e.returnValue = false }
  const res = addToDo.value.trim()
  const info = {
    res:res
  }
  users.push(info)
  add()
  addToDo.value = null
})
// if (users.length !== 1 ){res: res}else{res:users[users.length]}
/**
 * 编辑代办事项
 */
 function edit(){
  doingBox.addEventListener('click',(e) => {
      e = e || window.event
      const target = e.target || e.srcElement
      if(target.nodeName === 'P') {
          const edit = document.createElement('input')
          // console.log(edit);
          target.append(edit)
          edit.onchange = function(){
              target.innerText = this.value
              remove(edit)
          }
      }
  })
}
doingBox.addEventListener('change',(e) => {
  e = e || window.event
  const target = e.target || e.srcElement
  if(target.nodeName === 'INPUT') {
      if(target.checked){
          const index = target.dataset.index-0
          done.push(todoList[index])
          doingBox.removeChild(target.parentNode)
          blindDonelist()
      }
  }
})
/**
 * 移除代办事项
 */
 function remove(){
  doingBox.addEventListener('click',(e) => {
      e = e || window.event
      const target = e.target || e.srcElement
      if(target.nodeName === 'A'){
          doingBox.removeChild(target.parentNode)
      }
  })
}

/**
 * 跨域解决方案代码/配置粘贴处（请粘贴在块状注释范围内）

 */


